package com.lmiot.xyclick.Util;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import android.os.Build;
import com.lmiot.xyclick.Activity.IntentActivity;
import com.lkx.liandianqinew.R;
import com.xiaoyi.intentsdklibrary.Intent.Constants;
import java.util.Arrays;

public class ShortCutUtils {
    public static void addShortcut(Activity activity, String str, String str2, Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= 26) {
            addShortCutBitmap(activity, str, str2, bitmap);
            return;
        }
        try {
            Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intent.putExtra("duplicate", false);
            intent.putExtra("android.intent.extra.shortcut.NAME", "快捷方式1");
            intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Bitmap.createScaledBitmap(bitmap, 128, 128, true));
            Intent intent2 = new Intent("android.intent.action.MAIN");
            intent2.putExtra("flag", "executeAuto");
            intent2.putExtra("autoID", str2);
            intent2.setClass(activity, IntentActivity.class);
            intent2.addCategory("android.intent.category.LAUNCHER");
            intent.putExtra("android.intent.extra.shortcut.INTENT", intent2);
            activity.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void addShortcut(Activity activity, String str, String str2) {
        if (Build.VERSION.SDK_INT >= 26) {
            addShortCut(activity, str, str2);
            return;
        }
        try {
            Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intent.putExtra("duplicate", false);
            intent.putExtra("android.intent.extra.shortcut.NAME", "快捷方式1");
            intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(activity, R.drawable.play_auto_blue));
            Intent intent2 = new Intent("android.intent.action.MAIN");
            intent2.putExtra("flag", "executeAuto");
            intent2.putExtra("autoID", str2);
            intent2.setClass(activity, IntentActivity.class);
            intent2.addCategory("android.intent.category.LAUNCHER");
            intent.putExtra("android.intent.extra.shortcut.INTENT", intent2);
            activity.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void addShortCut(Context context, String str, String str2) {
        ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Constants.TYPE_CONTROL_SHORTCUT);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            Intent intent = new Intent(context, IntentActivity.class);
            intent.setAction("android.intent.action.VIEW");
            intent.putExtra("flag", "executeAuto");
            intent.putExtra("autoID", str2);
            ShortcutInfo build = new ShortcutInfo.Builder(context, str2).setIcon(Icon.createWithResource(context, (int) R.drawable.play_auto_blue)).setShortLabel(str).setIntent(intent).build();
            shortcutManager.requestPinShortcut(build, PendingIntent.getBroadcast(context, 0, new Intent(context, IntentActivity.class), 134217728).getIntentSender());
            try {
                shortcutManager.updateShortcuts(Arrays.asList(build));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void addShortCutBitmap(Context context, String str, String str2, Bitmap bitmap) {
        ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(Constants.TYPE_CONTROL_SHORTCUT);
        if (shortcutManager.isRequestPinShortcutSupported()) {
            Intent intent = new Intent(context, IntentActivity.class);
            intent.setAction("android.intent.action.VIEW");
            intent.putExtra("flag", "executeAuto");
            intent.putExtra("autoID", str2);
            ShortcutInfo build = new ShortcutInfo.Builder(context, str2).setIcon(Icon.createWithBitmap(Bitmap.createScaledBitmap(bitmap, 128, 128, true))).setShortLabel(str).setIntent(intent).build();
            shortcutManager.requestPinShortcut(build, PendingIntent.getBroadcast(context, 0, new Intent(context, IntentActivity.class), 134217728).getIntentSender());
            try {
                shortcutManager.updateShortcuts(Arrays.asList(build));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
